﻿using GameFramework;
using System;
using UnityEngine;
using XLua;

namespace GFTest
{
    /// <summary>
    /// 使用 Lua 脚本的 NGUISubForm。
    /// </summary>
    [XLua.LuaCallCSharp]
    public class LuaNGUISubForm : NGUISubForm
    {
        #region 控件序列化字段

        [SerializeField]
        private string m_LuaScriptName = null;

        [SerializeField]
        private LuaNGUIElement[] m_UIElements = null;

        [SerializeField]
        private GameObject[] m_GameObjects = null;

        [SerializeField]
        private Transform[] m_Transforms = null;

        [SerializeField]
        private UIPanel[] m_UIPanels = null;

        [SerializeField]
        private UILabel[] m_UILabels = null;

        [SerializeField]
        private UIInput[] m_UIInputs = null;

        [SerializeField]
        private UIButton[] m_UIButtons = null;

        [SerializeField]
        private UIToggle[] m_UIToggles = null;

        [SerializeField]
        private UISprite[] m_UISprites = null;

        [SerializeField]
        private UITexture[] m_UITextures = null;

        [SerializeField]
        private UIProgressBar[] m_UIProgressBars = null;

        [SerializeField]
        private UISlider[] m_UISliders = null;

        [SerializeField]
        private UIScrollBar[] m_UIScrollBars = null;

        [SerializeField]
        private UIScrollView[] m_UIScrollViews = null;

        [SerializeField]
        private UIGrid[] m_UIGrids = null;

        [SerializeField]
        private UITable[] m_UITables = null;

        #endregion 控件序列化字段

        #region Lua定义

        private LuaTable m_LuaTable = null;
        private Action<object> m_LuaOnInit = null;
        private Action<object> m_LuaOnOpen = null;
        private Action<object> m_LuaOnClose = null;
        private Action<object> m_LuaOnEvent = null;
        private Action<GameObject> m_LuaOnClick = null;
        private Action<GameObject> m_LuaOnToggle = null;
        private Action<GameObject> m_LuaOnInputChange = null;
        private Action<GameObject> m_LuaOnPress = null;
        private Action<GameObject> m_LuaOnRelease = null;
        private Action<GameObject> m_LuaOnDragOver = null;
        private Action<GameObject> m_LuaOnDragOut = null;
        private Action m_LuaOnDestroy = null;

        #endregion Lua定义

        #region 控件属性

        public string LuaScriptName
        {
            get
            {
                return m_LuaScriptName;
            }
        }

        public LuaNGUIElement[] UIElements
        {
            get
            {
                return m_UIElements;
            }
        }

        public GameObject[] GameObjects
        {
            get
            {
                return m_GameObjects;
            }
        }

        public Transform[] Transforms
        {
            get
            {
                return m_Transforms;
            }
        }

        public UIPanel[] UIPanels
        {
            get
            {
                return m_UIPanels;
            }
        }

        public UILabel[] UILabels
        {
            get
            {
                return m_UILabels;
            }
        }

        public UIInput[] UIInputs
        {
            get
            {
                return m_UIInputs;
            }
        }

        public UIButton[] UIButtons
        {
            get
            {
                return m_UIButtons;
            }
        }

        public UIToggle[] UIToggles
        {
            get
            {
                return m_UIToggles;
            }
        }

        public UISprite[] UISprites
        {
            get
            {
                return m_UISprites;
            }
        }

        public UITexture[] UITextures
        {
            get
            {
                return m_UITextures;
            }
        }

        public UISlider[] UISliders
        {
            get
            {
                return m_UISliders;
            }
        }

        public UIProgressBar[] UIProgressBars
        {
            get
            {
                return m_UIProgressBars;
            }
        }

        public UIScrollView[] UIScrollViews
        {
            get
            {
                return m_UIScrollViews;
            }
        }

        public UIScrollBar[] UIScrollBars
        {
            get
            {
                return m_UIScrollBars;
            }
        }

        public UIGrid[] UIGrids
        {
            get
            {
                return m_UIGrids;
            }
        }

        public UITable[] UITables
        {
            get
            {
                return m_UITables;
            }
        }

        #endregion 控件属性

        #region 界面方法

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            if (string.IsNullOrEmpty(m_LuaScriptName))
            {
                Log.Error("Lua script name is invalid.");
                return;
            }

            m_LuaTable = GameEntry.Lua.NewTable();
            if (m_LuaTable == null)
            {
                Log.Error("Lua table is invalid.");
                return;
            }

            GameEntry.Lua.DoScript("UI/LuaNGUIFormFactory", m_LuaScriptName, m_LuaTable);
            m_LuaTable.Set("self", this);
            m_LuaTable.Set("owner", OwnerUIForm);
            m_LuaTable.Get("OnInit", out m_LuaOnInit);
            m_LuaTable.Get("OnOpen", out m_LuaOnOpen);
            m_LuaTable.Get("OnClose", out m_LuaOnClose);
            m_LuaTable.Get("OnEvent", out m_LuaOnEvent);
            m_LuaTable.Get("OnClick", out m_LuaOnClick);
            m_LuaTable.Get("OnToggle", out m_LuaOnToggle);
            m_LuaTable.Get("OnInputChange", out m_LuaOnInputChange);
            m_LuaTable.Get("OnPress", out m_LuaOnPress);
            m_LuaTable.Get("OnRelease", out m_LuaOnRelease);
            m_LuaTable.Get("OnDragOver", out m_LuaOnDragOver);
            m_LuaTable.Get("OnDragOut", out m_LuaOnDragOut);
            m_LuaTable.Get("OnDestroy", out m_LuaOnDestroy);

            if (m_LuaOnInit != null)
            {
                m_LuaOnInit(userData);
            }
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            if (m_LuaOnOpen != null)
            {
                m_LuaOnOpen(userData);
            }
        }

        protected override void OnClose(object userData)
        {
            if (m_LuaOnClose != null)
            {
                m_LuaOnClose(userData);
            }

            base.OnClose(userData);
        }

        public void OnEvent(object userData)
        {
            if (m_LuaOnEvent != null)
            {
                m_LuaOnEvent(userData);
            }
        }

        public void OnClick(GameObject gameObject)
        {
            if (m_LuaOnClick != null)
            {
                m_LuaOnClick(gameObject);
            }
        }

        public void OnToggle(GameObject gameObject)
        {
            if (m_LuaOnToggle != null)
            {
                m_LuaOnToggle(gameObject);
            }
        }

        public void OnInputChange(GameObject gameObject)
        {
            if (m_LuaOnInputChange != null)
            {
                m_LuaOnInputChange(gameObject);
            }
        }

        public void OnPress(GameObject gameObject)
        {
            if (m_LuaOnPress != null)
            {
                m_LuaOnPress(gameObject);
            }
        }

        public void OnRelease(GameObject gameObject)
        {
            if (m_LuaOnRelease != null)
            {
                m_LuaOnRelease(gameObject);
            }
        }

        public void OnDragOver(GameObject gameObject)
        {
            if (m_LuaOnDragOver != null)
            {
                m_LuaOnDragOver(gameObject);
            }
        }

        public void OnDragOut(GameObject gameObject)
        {
            if (m_LuaOnDragOut != null)
            {
                m_LuaOnDragOut(gameObject);
            }
        }

        private void OnDestroy()
        {
            if (m_LuaOnDestroy != null)
            {
                m_LuaOnDestroy();
            }

            m_LuaOnInit = null;
            m_LuaOnOpen = null;
            m_LuaOnClose = null;
            m_LuaOnEvent = null;
            m_LuaOnClick = null;
            m_LuaOnToggle = null;
            m_LuaOnInputChange = null;
            m_LuaOnPress = null;
            m_LuaOnRelease = null;
            m_LuaOnDragOver = null;
            m_LuaOnDragOut = null;
            m_LuaOnDestroy = null;

            if (m_LuaTable != null)
            {
                m_LuaTable.Dispose();
                m_LuaTable = null;
            }
        }

        #endregion 界面方法
    }
}
